<?php
chdir ("..");
include "t16cms/php/config.php";
include "t16cms/php/user.php";
include "forum/functions.php";
include "forum/xstyle.php";

if (isset($_GET['page'])){
$forum_page=$_GET['page'];
}else{
$forum_page="1";
}

if (!isset($_GET['topic'])){


mysql_connect($dbhost, $dbuser, $dbpasswd);
mysql_query('SET NAMES cp1251');

// Geting all categories
if (isset($_GET['category'])){


$categor=htmlspecialchars(htmlentities($_GET['category']));
$query='SELECT COUNT( * ) FROM `'.$dbprefix.'forum_categories` where `parent`='."'".$categor."'";

$result=mysql_db_query($dbname, $query);
while($row=mysql_fetch_array($result)) {
$forum_topic_count=$row['0'];
};
mysql_free_result($result);


if ($forum_topic_count>$forum_pcnt){

//echo "all:".$forum_topic_count." page:".$forum_page."    ";
$ngpage= "LIMIT ".($forum_page-1)*$forum_pcnt.",".$forum_page*$forum_pcnt;

$query="select * from `".$dbprefix."forum_categories` ".' where `parent`='."'".$categor."'"." ORDER BY `pos` ASC ".$ngpage;

// ok, query completed
// now we will create page menu

$forum_pages=pages_start();
$d=1;
for ($i=1;$i<=$forum_topic_count;$i=$i+$forum_pcnt){
$forum_pages=$forum_pages.page($d,'category='.$categor);
$d++;
}
$forum_pages=$forum_pages.pages_end();


}else{
$query="select * from `".$dbprefix."forum_categories` ".' where `parent`='."'".$categor."'"." ORDER BY `pos` ASC LIMIT 0,".$forum_topic_count;
}
}else{
$query="select * from `".$dbprefix."forum_categories` ORDER BY `pos` ASC ";
}

$result=mysql_db_query($dbname, $query);
$i=1;
while($row=mysql_fetch_array($result)) {
  $forum_cat[$i]=$row;
  $i++;

};
mysql_free_result($result);
$cnt=$i;
// now we will format all




// ok, let's see to _get data =)

$site_output="";
if (isset($_GET['category'])){
$top=htmlentities($_GET['category']);
// ok they need one category





// here we will show all topics in category
categories($top,false);


ob_start();
if (get_reg()==1) {
// ok, we got in
if (get_right($site_user['access'],1)) {
//ok we at least member
// link to add new post in topic
$site_output=$site_output.admins('<a href="'.$site_url.'/forum/addtopic.php?authkey='.$_GET['authkey'].'&top='.$top.'"> new topic</a>');

}
}
ob_end_clean();


}else{
$top="";
categories($top,true);

ob_start();
get_reg();
ob_end_clean();

if (get_right($site_user['access'],6)) {
$site_output=$site_output.admins('<a href="'.$site_url.'/forum/admin.php?authkey='.$_GET['authkey'].'">Adminko</a>');
}
}
}else{


mysql_connect($dbhost, $dbuser, $dbpasswd);
mysql_query('SET NAMES cp1251');
$query="select * from `".$dbprefix."users`";
$result=mysql_db_query($dbname, $query);
$i=1;
while($row=mysql_fetch_array($result)) {
  $site_users_arr[$row['name']]=$row;
  $i=$i+1;
};
mysql_free_result($result);

mysql_query('SET NAMES cp1251');
// ok, we need to echo topic
$topic=htmlentities($_GET['topic']);
//
$categor=htmlspecialchars(htmlentities($_GET['category']));
$query='SELECT COUNT( * ) FROM `'.$dbprefix.'forum_posts` where `parent`='."'".$topic."'";

$result=mysql_db_query($dbname, $query);
while($row=mysql_fetch_array($result)) {
$forum_posts_count=$row['0'];
};
mysql_free_result($result);

mysql_connect($dbhost, $dbuser, $dbpasswd);
//
//;
if ($forum_posts_count>$forum_posts_pcnt){
$forum_pages=pages_start();
$d=1;

for ($i=1;$i<=$forum_posts_count;$i=$i+$forum_posts_pcnt){
$forum_pages=$forum_pages.page($d,'topic='.$topic);
$d++;
}
$forum_pages=$forum_pages.pages_end();
//
}
$ngpage= "LIMIT ".($forum_page-1)*$forum_posts_pcnt.",".$forum_page*$forum_posts_pcnt;
//
$query="select * from `".$dbprefix."forum_posts` where `parent`='".$topic."' ORDER BY `pos` ASC ".$ngpage;


$result=mysql_db_query($dbname, $query);
$i=1;
$site_output=posts_start($topic);
while($row=mysql_fetch_array($result)) {
  $forum_posts[$i]=$row;
  $site_output=$site_output.post($row);
  $i++;

};
$site_output=$site_output.posts_end($topic);
mysql_free_result($result);
$cnt=$i;



ob_start();
if (get_reg()==1) {
// ok, we got in
if (get_right($site_user['access'],1)) {
//ok we at least member
// link to add new post in topic
$site_output=$site_output.admins('<a href="'.$site_url.'/forum/addpost.php?authkey='.$_GET['authkey'].'&topic='.$_GET['topic'].'"> new post</a>');

}
}
ob_end_clean();





}






include "forum/menu.php";
include "forum/style.php";
?>